package com.zbl.zblproject.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.entity.Supplier;
import com.zbl.zblproject.core.mapper.SupplierMapper;
import com.zbl.zblproject.core.service.SupplierService;
import com.zbl.zblproject.core.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/12
 */
@RestController
@RequestMapping("/supplier")
@Api(value = "供应商接口", tags = {"供应商接口"})
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private SupplierMapper supplierMapper;

    private static final Logger logger = LoggerFactory.getLogger(SupplierController.class);


    //供应商付款
    @PostMapping("/recharge")
    @ApiOperation("用户充值")
    public Result recharge(@ApiParam("供应商ID") @RequestParam("supplierId") int supplierId,
                           @ApiParam("操作人ID") @RequestParam("operatorId") int operatorId,
                           @ApiParam("充值金额") @RequestParam("price") double price){

        try{
            if(supplierService.recharge(supplierId, operatorId, price)){
                return ResultUtil.success("成功!");
            }
            return ResultUtil.error("失败");
        }catch(RuntimeException e){
            return ResultUtil.error(e.getMessage());
        }
    }


    //供应商付款记录
    @GetMapping("/recharge-log/{supplierId}/{current}/{size}")
    @ApiOperation("充值记录")
    public Result rechargeLog(@ApiParam("供应商ID") @PathVariable int supplierId,
                              @ApiParam("页数") @PathVariable int current,
                              @ApiParam("数量") @PathVariable int size){
        return ResultUtil.success("成功", supplierService.logPage(supplierId, current, size));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取单个供应商")
    public Result one(@ApiParam("供应商ID") @PathVariable Integer id){
        if(id > 0){
            return ResultUtil.success("成功!", supplierService.getOneById(id));
        }
        return ResultUtil.error("失败!");
    }

    @GetMapping("/list/{current}/{size}")
    @ApiOperation("供应商列表")
    public Result list(@ApiParam("查询页数") @PathVariable Integer current,
                       @ApiParam("查询数量") @PathVariable Integer size){
          return ResultUtil.success("成功!", supplierService.erpSupperList(current, size));
    }

    @GetMapping("/list-all")
    @ApiOperation("全部供应商列表")
    public Result listAll(){
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        //wrapper.select("suppliers_id", "suppliers_name");
        return ResultUtil.success("成功!", supplierMapper.selectList(wrapper));
    }


    @PostMapping("/add")
    @ApiOperation("添加一个供应商")
    public Result addSupplier(Supplier supplier){
        logger.info("供应商信息:{}", JSONObject.toJSON(supplier));
        try {
            supplierService.addSupplier(supplier);
            return ResultUtil.success("成功!", supplier);
        }catch(Exception e){
            return ResultUtil.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    @ApiOperation("更新供应商")
    public Result updateSupplier(Supplier supplier){
        logger.info("供应商信息:{}", JSONObject.toJSON(supplier));
        
        if(supplier.getSuppliersId() <= 0){
            return ResultUtil.error("供应商ID为空.");
        }

        if(supplierService.updateSupplier(supplier)){
            return ResultUtil.success("更新成功!");
        }
        return ResultUtil.error("更新失败!");
    }


    @GetMapping("/delete/some")
    @ApiOperation("删除一群供应商")
    public Result delete(@ApiParam("供应商ID集合") @RequestParam("ids") Integer[] array){
        List<Integer> list = Arrays.asList(array);
        if(list.isEmpty()){
            return ResultUtil.error("商品ID异常!");
        }
        if(supplierService.deleteSuppliers(list)){
            return ResultUtil.success("删除成功!");
        }else{
            return ResultUtil.error("删除失败!");
        }
    }


}
